package com.dingwei.lesyndrome.mvp.presenter;

import android.app.Dialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.base.FailsBean;
import com.dingwei.lesyndrome.base.MyBaseBean;
import com.dingwei.lesyndrome.bean.PersonalInfoBean;
import com.dingwei.lesyndrome.custom.myCallback;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.dingwei.lesyndrome.ui.activity.PersonalInfoActivity;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;
import com.lihao.baselibrary.weight.WheelView;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lihao on 2017/9/12.
 */

public class PersonalInfoPresenter extends basePresenter {

    private Contract.PersonalInfoView personalInfoView;
    private Dialog sexDialog;//性别弹出框
    public PersonalInfoPresenter(Contract.PersonalInfoView view) {
        super(view);
        this.personalInfoView = view;
    }

    public void initData() {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.USER_CENTER);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        x.http().post(rp, new myCallback(BaseActivity.context, PersonalInfoBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                PersonalInfoBean bean = (PersonalInfoBean) baseBean;
                personalInfoView.setData(bean);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                personalInfoView.onFail();
            }
        });
    }

    /**
     * 上传头像
     * @param lastPath
     */
    public void uploadHeadImg(String lastPath) {
        RequestParams rp =new RequestParams(URLS.HOST+URLS.SAVE_DATA);
        rp.addBodyParameter("sessionid",BaseActivity.value.getToken());
        rp.addBodyParameter("hportrait",new File(lastPath));
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class,rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast("保存成功");
                personalInfoView.uploadPicSuccess();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }


    /**
     * 性别选择框
     * @param activity
     * @param sex 目前的性别
     */
    public void showChooseSexDialog(PersonalInfoActivity activity, final String sex) {

        List<String> list = new ArrayList<>();
        list.add("男");
        list.add("女");
        list.add("保密");
        View view = LayoutInflater.from(BaseActivity.context).inflate(R.layout.dialog_check_sex, null);
        TextView tvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        TextView tvSubmit = (TextView) view.findViewById(R.id.tv_submit);
        final WheelView wheelView = (WheelView) view.findViewById(R.id.wheel);
        wheelView.setOffset(2);
        wheelView.setItems(list);
        view.measure(0,0);
        if (sex.equals("男")){
            wheelView.setSeletion(0);
        }else if (sex.equals("女")){
            wheelView.setSeletion(1);
        }else {
            wheelView.setSeletion(2);
        }
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sexDialog.dismiss();
            }
        });
        tvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sexDialog.dismiss();

                if (!wheelView.getSeletedItem().equals(sex)){
                    personalInfoView.ChooseSex(wheelView.getSeletedItem());
                }else {
                    showToast("保存成功");
                }
            }
        });
        sexDialog = new Dialog(activity,R.style.Theme_Light_Dialog);
        sexDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        sexDialog.setContentView(view);
        Window dialogWindow = sexDialog.getWindow();
        //设置dialog在屏幕底部
        dialogWindow.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        dialogWindow.setWindowAnimations(R.style.dialogstyle);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        android.view.WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        dialogWindow.setAttributes(lp);
        sexDialog.show();
    }

    /**
     * 保存用户性别
     * @param gender
     */
    public void saveGender(final String gender) {

        RequestParams rp =new RequestParams(URLS.HOST+URLS.SAVE_DATA);
        rp.addBodyParameter("sessionid",BaseActivity.value.getToken());
        switch (gender){
            case "保密":
                rp.addBodyParameter("sex","0");
                break;
            case "男":
                rp.addBodyParameter("sex","1");
                break;
            case "女":
                rp.addBodyParameter("sex","2");
                break;
        }
        x.http().post(rp, new myCallback(BaseActivity.context,MyBaseBean.class,rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast(baseBean.message);
                personalInfoView.saveGenderSuccess(gender);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }
}
